class Solution {
    public int maxDistance(List<List<Integer>> arrays) {
        int ta, tb;
        int n = arrays.size();

        ta = arrays.get(0).get(arrays.get(0).size() - 1);
        tb = arrays.get(0).get(0);

        int ans = 0;
        int ca, cb;
        for (int i = 1; i < n; i++) {

            ca = arrays.get(i).get(arrays.get(i).size() - 1);
            cb = arrays.get(i).get(0);

            ans = Math.max(ans, Math.max(Math.abs(ca - tb), Math.abs(cb - ta)));
            ta = Math.max(ta, ca);
            tb = Math.min(tb, cb);
        }
        return ans;
    }
}